package cn.ixinjiu.libraryMangement.controller.systemAdmin.borrow;

import cn.ixinjiu.libraryMangement.service.IBorrowInfoService;
import cn.ixinjiu.libraryMangement.service.impl.BorrowInfoServiceImpl;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by XinChen on 2023-05-12
 *
 * @TODO 借阅信息删除
 */
@WebServlet("/borrowInfoDel")
public class BorrowInfoDelServlet extends HttpServlet {

    private IBorrowInfoService iBorrowInfoService = new BorrowInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        System.out.println("-----------------  /borrowInfoDel  ----------------------");
        JSONObject jsonObject = new JSONObject(); // 返回的json对象
        int code; // 状态码
        String msg; // 提示信息

        String id = req.getParameter("id");
        System.out.println("id = " + id);

        int i = iBorrowInfoService.borrowInfoDel(Integer.parseInt(id));
        if (i == 1) { // 删除成功
            code = 0;
            msg = "删除成功";
        }else {
            code = 1;
            msg = "删除失败";
        }

        jsonObject.put("code", code);
        jsonObject.put("msg", msg);

        PrintWriter writer = resp.getWriter();
        writer.write(jsonObject.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
